//FACEBOOK ASYNC CLIENT CLASS IMPLEMENTATION

#include "facebookasync.h"

#include "xmlParser.h"
#include "md5.h"
#include <fstream>

//CURRENT USER ROUTINES

bool FacebookAsync::CurUser_GetUserID(std::string& Result)
{
	Result = "";
	//checking if init was called
	if (m_AppKey.size() < 1)
	{
		m_ErrorDescription = "Facebook::CurUser_GetUserID(). Facebook::Init() wasn't called!"; return false;
	}

	//checking if cur user is ok
	if (m_pCurUser == NULL)
	{
		m_ErrorDescription = "Facebook::CurUser_GetUserID(). No current user was set!"; return false;
	}

	if (m_pCurUser->m_CurLoginStage != LOGINSTAGE_SUCCEED)
	{
		m_ErrorDescription = "Facebook::CurUser_GetUserID(). Current user is not logged in!"; return false;
	}

	Result = m_pCurUser->m_UserID;
	m_ErrorDescription = "";
	return true;

}//bool FacebookAsync::CurUser_GetUserID(std::string& Result)



bool FacebookAsync::CurUser_GetUserInfo(FACEBOOK_PROFILEINFO InfoField, std::string& Result)
{

	Result = "";
	//checking if init was called
	if (m_AppKey.size() < 1)
	{
		m_ErrorDescription = "FacebookAsync::CurUser_GetUserInfo(). Facebook::Init() wasn't called!"; return false;
	}

	//checking if cur user is ok
	if (m_pCurUser == NULL)
	{
		m_ErrorDescription = "FacebookAsync::CurUser_GetUserInfo(). No current user was set!"; return false;
	}

	if (m_pCurUser->m_CurInfoStage == INFOSTAGE_NOTSTARTED || m_pCurUser->m_CurInfoStage == INFOSTAGE_FAILED || m_pCurUser->m_CurInfoStage == INFOSTAGE_GETTINGUSERINFO)
	{
		m_ErrorDescription = "FacebookAsync::CurUser_GetUserInfo(). Info wasn't fetched!"; return false;
	}

	Helper_FillInfoFieldFromProfile(InfoField, m_pCurUser->m_UserProfileInfo, Result);  
	m_ErrorDescription = "";
	return true;

}//bool FacebookAsync::CurUser_GetUserInfo(FACEBOOK_PROFILEINFO InfoField, std::string& Result)



int FacebookAsync::CurUser_GetFriendsCount()
{
	if (m_pCurUser == NULL)
		return -1;

	if (m_pCurUser->m_FriendsIDs.empty() == true)
		return 0;
	else
		return (int) m_pCurUser->m_FriendsIDs.size();

}//int FacebookAsync::CurUser_GetFriendsCount()



bool FacebookAsync::CurUser_GetFriendID(int FriendIndex, std::string& Result)
{
	Result = "";
	//checking if init was called
	if (m_AppKey.size() < 1)
	{
		m_ErrorDescription = "FacebookAsync::CurUser_GetFriendID(). Facebook::Init() wasn't called!"; return false;
	}

	//checking if cur user is ok
	if (m_pCurUser == NULL)
	{
		m_ErrorDescription = "FacebookAsync::CurUser_GetFriendID(). No current user was set!"; return false;
	}

	if (m_pCurUser->m_CurInfoStage == INFOSTAGE_NOTSTARTED || m_pCurUser->m_CurInfoStage == INFOSTAGE_FAILED || m_pCurUser->m_CurInfoStage == INFOSTAGE_GETTINGUSERINFO || m_pCurUser->m_CurInfoStage == INFOSTAGE_GETTINGFRIENDSINFO)
	{
		m_ErrorDescription = "FacebookAsync::CurUser_GetFriendID(). Info wasn't fetched!"; return false;

	}

	if (FriendIndex < 0 || FriendIndex >= CurUser_GetFriendsCount())
	{
		m_ErrorDescription = "FacebookAsync::CurUser_GetFriendID(). Invalid FriendIndex value!"; return false;
	}

    Result = m_pCurUser->m_FriendsIDs.at(FriendIndex);
	m_ErrorDescription = "";
	return true;

}//bool FacebookAsync::CurUser_GetFriendID(int FriendIndex, std::string& Result)



bool FacebookAsync::CurUser_GetFriendInfo(int FriendIndex, FACEBOOK_PROFILEINFO InfoField, std::string& Result)
{

	Result = "";
	//checking if init was called
	if (m_AppKey.size() < 1)
	{
		m_ErrorDescription = "FacebookAsync::CurUser_GetFriendInfo(). Facebook::Init() wasn't called!"; return false;
	}

	//checking if cur user is ok
	if (m_pCurUser == NULL)
	{
		m_ErrorDescription = "FacebookAsync::CurUser_GetFriendInfo(). No current user was set!"; return false;
	}

	if (m_pCurUser->m_CurInfoStage != INFOSTAGE_SUCCEED)
	{
		m_ErrorDescription = "FacebookAsync::CurUser_GetFriendInfo(). Info wasn't fetched!"; return false;

	}

	if (FriendIndex < 0 || FriendIndex >= CurUser_GetFriendsCount())
	{
		m_ErrorDescription = "FacebookAsync::CurUser_GetFriendInfo(). Invalid FriendIndex value!"; return false;
	}

    Helper_FillInfoFieldFromProfile(InfoField, m_pCurUser->m_FriendsProfileInfo.at(FriendIndex), Result); 
	m_ErrorDescription = "";
	return true;

}//bool FacebookAsync::CurUser_GetFriendInfo(int FriendIndex, FACEBOOK_PROFILEINFO InfoField, std::string& Result)



int FacebookAsync::CurUser_UserPhoto_GetSize()
{
	if (m_pCurUser == NULL)
		return -1;

	return (int) m_pCurUser->m_UserPhoto.size(); 

}//int FacebookAsync::CurUser_UserPhoto_GetSize()



const std::string* FacebookAsync::CurUser_UserPhoto_GetData()
{

	if (m_pCurUser == NULL)
		return NULL;

	return &(m_pCurUser->m_UserPhoto); 

}//const std::string* FacebookAsync::CurUser_UserPhoto_GetData()




int FacebookAsync::CurUser_FriendPhoto_GetSize(int FriendIndex)
{
	if (m_pCurUser == NULL)
		return -1;

	if (FriendIndex < 0 || FriendIndex >= CurUser_GetFriendsCount())
		return -1;

	return (int) m_pCurUser->m_FriendsPhotos.at(FriendIndex).size();

}//int FacebookAsync::CurUser_FriendPhoto_GetSize(int FriendIndex)



const std::string* FacebookAsync::CurUser_FriendPhoto_GetData(int FriendIndex)
{

	if (m_pCurUser == NULL)
		return NULL;

	if (FriendIndex < 0 || FriendIndex >= CurUser_GetFriendsCount())
		return NULL;

	return &(m_pCurUser->m_FriendsPhotos.at(FriendIndex)); 

}//const std::string* FacebookAsync::CurUser_FriendPhoto_GetData(int FriendIndex)




int FacebookAsync::CurUser_UserPhoto_GetWidth()
{
	if (m_pCurUser == NULL)
		return 0;

	return m_pCurUser->m_UserProfileInfo.PhotoSize.Width;   
}



int FacebookAsync::CurUser_UserPhoto_GetHeight()
{
	if (m_pCurUser == NULL)
		return 0;

	return m_pCurUser->m_UserProfileInfo.PhotoSize.Height;   
}



int FacebookAsync::CurUser_FriendPhoto_GetWidth(int FriendIndex)
{
	if (m_pCurUser == NULL)
		return 0;

	if (FriendIndex < 0 || FriendIndex >= CurUser_GetFriendsCount())
		return 0;

	return m_pCurUser->m_FriendsProfileInfo.at(FriendIndex).PhotoSize.Width;
}



int FacebookAsync::CurUser_FriendPhoto_GetHeight(int FriendIndex)
{
	if (m_pCurUser == NULL)
		return 0;

	if (FriendIndex < 0 || FriendIndex >= CurUser_GetFriendsCount())
		return 0;

	return m_pCurUser->m_FriendsProfileInfo.at(FriendIndex).PhotoSize.Height;
}




//app and non-app friends routines

int FacebookAsync::CurUser_AppFriends_GetCount()
{
	if (m_pCurUser == NULL)
		return -1;

	return (int)m_pCurUser->m_AppFriendsIndexes.size();

}



int FacebookAsync::CurUser_AppFriends_GetCommonIndex(int AppFriendIndex)
{
	if (m_pCurUser == NULL)
		return -1;

	if (m_pCurUser->m_AppFriendsIndexes.empty() == true)
		return -1;

	if (AppFriendIndex < 0 || AppFriendIndex >= (int)m_pCurUser->m_AppFriendsIndexes.size())
		return -1;

	return m_pCurUser->m_AppFriendsIndexes.at(AppFriendIndex); 

}



int FacebookAsync::CurUser_NonAppFriends_GetCount()
{
	if (m_pCurUser == NULL)
		return -1;

	return (int)m_pCurUser->m_NonAppFriendsIndexes.size();

}



int FacebookAsync::CurUser_NonAppFriends_GetCommonIndex(int NonAppFriendIndex)
{
	if (m_pCurUser == NULL)
		return -1;

	if (m_pCurUser->m_NonAppFriendsIndexes.empty() == true)
		return -1;

	if (NonAppFriendIndex < 0 || NonAppFriendIndex >= (int)m_pCurUser->m_NonAppFriendsIndexes.size())
		return -1;

	return m_pCurUser->m_NonAppFriendsIndexes.at(NonAppFriendIndex); 

}
